<?php
session_start();

/***************************************************************\
* FILE:		page.php
* SECTION:	core
* FUNCTION:	main page classification
* USES:		server variables $_SERVER[]
* INCLUDES:	/sys/login.inc
*		/sys/access.inc
*		/sys/dbfunctions.inc
*		/sys/functions.inc
*		/sys/config.inc
* PRODUCES:	main enviromental variables $PAGE
* LAUNCHES:	/sys/pages/{$PAGE['type']}.inc
\***************************************************************/

// 0. include primary files
include_once("sys/functions.inc");
$start = getmicrotime();
include_once("sys/config.inc");
include_once("sys/dbfunctions.inc");
include_once("sys/formfunctions.inc");
include_once("sys/mailfunctions.inc");
include_once("sys/login.inc");
include_once("sys/access.inc");

global $CONFIG, $PAGE, $ACCESSLIST, $no_menu;

$PAGE = array();
$ACCESSLIST = array();

// 1. Start session
if ($_GET['action'] == "logout") {
	logout();
	unset($_GET['action']);
} else if ($_SESSION['user_id'] == "" && ($_GET['action'] == "login" || $_COOKIE[$CONFIG['cookie_name']] != "")) {
	login();
	if ($_GET['action'] == "login")
		unset($_GET['action']);
}

// 2. Derive page location from URL
calculate_page($_SERVER['PATH_INFO']);
fetch_moderators($PAGE['directory_tree'][$PAGE['dir_depth']-1]['dir_id']);
include_once("sys/pages/{$PAGE['type']}.inc");

// 3. Generate menu
if (!$no_menu)
	generate_menu();

// 4. Check access
generate_accesslist();
//printAL();
check_access("VIEW");
page_inc_check_access();
if (has_access("ADMIN"))
	add_action('admin', 'url', '.admin');

// 5. Execute page logic
if ($_GET['action'])
	page_inc_execute_action($_GET['action']);
page_inc_execute();

// 6. Check mail & messages
if ($_SESSION['user_id']) {
	fetch_alerts();
	fetch_mail();
}

// 6.5 who's online !
if ($_SESSION['member']['pref_last_clicks'])
	$PAGE['last_clicks'] = get_last_clicks();
if ($_SESSION['user_id'])
	click();

// 7. Dress up content
// load skin
include_once("sys/skins/{$CONFIG['default_skin']}/main.inc");

// default printing
print_header($PAGE['menu'], $PAGE['content']['title'], $no_menu, $PAGE['last_clicks'], $PAGE['moderators']);
if (!$no_menu)
	print_actions($PAGE['actions']);
print_title($PAGE['content']['title']);
print_alerts($PAGE['alerts'], $PAGE['alerts_delete']);
if (!$no_menu)
	print_mailform(mail_form(), $_POST['mailing'] or $_GET['mailto']);

// apply skin
page_inc_dress();

// default printing
print_footer($PAGE['body_footer'], (round(getmicrotime()-$start,4)), $no_menu, $PAGE['last_clicks']);

finish();

?>
